﻿/***********************************************************************************************
 * 
 * ProjectName: WMSSoft.WinKStart
 * 
 * Description: Provide the ViewModel used in Dialog Settings_DataSourceExtension
 * 
 * Created:     04.09.2015
 * 
 * Author:      matthias wagler - matthias@WMSSoft.de
 * 
 * Copyright:   Copyright 2015 by WMSSoft.de
 * 
 * License:     GPLv3
 * 
 * History:     04.09.2015 - matthias wagler matthias@WMSSoft.de   -   Created
 * 
 **********************************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WMSSoft.Lib.Basic;
using WMSSoft.Lib.Texte;
using WMSSoft.Lib.WinKStart;
using WMSSoft.Lib.WinKStart.Classes.Metadata;
using WMSSoft.Lib.WinKStart.Abstract;
using WMSSoft.Lib.WinKStart.Model;
using WMSSoft.Lib.WinKStart.ViewModel;

namespace WMSSoft.WinKStart.ViewModel
{
    public class Settings_NewEditDataSourceExtensionViewModel : SettingBaseListViewModel
    {
        KeyValueAssignment _Data = null;
        OptionInfo _OptionInfo = null;
        System.Windows.Visibility _BooleanValueVisible = System.Windows.Visibility.Collapsed;
        System.Windows.Visibility _ListValueVisible = System.Windows.Visibility.Collapsed;
        System.Windows.Visibility _NumericValueVisible = System.Windows.Visibility.Collapsed;
        System.Windows.Visibility _NumericDoubleValueVisible = System.Windows.Visibility.Collapsed;
        System.Windows.Visibility _TextValueVisible = System.Windows.Visibility.Visible;
        List<KeyValueAssignment> _AvailableListValues = null;
        string _ActiveListValue = "";
        string _KeyValue = "";
        string _TextboxValue = "";
        bool _BoolValue = false;
        bool _DataChanged = false;
        bool _CanChangeAssignmentKey = false;
        double _NumericDoubleValue = 0.00;
        int _NumericValue = 0;

        #region Initialize
        public Settings_NewEditDataSourceExtensionViewModel(OptionInfo ValueInfo)
        {
            try
            {
                base.ViewName = LanguageStrings.GetText("New Key - Value - Assignment");

                PropertyChanged += Settings_NewEditDataSourceExtensionViewModel_PropertyChanged;
                ViewModel_OkClick += Settings_NewEditDataSourceExtensionViewModel_ViewModel_OkClick;
                ViewModel_CloseClick += Settings_NewEditDataSourceExtensionViewModel_ViewModel_CloseClick;

                AvailableListValues = new List<KeyValueAssignment>();

                Data = new KeyValueAssignment("","");
                Option = ValueInfo;

            }
            catch (Exception Ex)
            {
                throw new Exception("Initialize DataSource Extension-Dialog ViewModel failed!", Ex);
            }
        }

        public Settings_NewEditDataSourceExtensionViewModel(KeyValueAssignment AssignmentItem, OptionInfo ValueInfo)
        {
            try 
            {
                if (ValueInfo == null)
                    base.ViewName = LanguageStrings.GetText("Edit Key - Value - Assignment");
                else
                    base.ViewName = "Change Option-Value";



                PropertyChanged += Settings_NewEditDataSourceExtensionViewModel_PropertyChanged;
                ViewModel_OkClick += Settings_NewEditDataSourceExtensionViewModel_ViewModel_OkClick;
                ViewModel_CloseClick += Settings_NewEditDataSourceExtensionViewModel_ViewModel_CloseClick;

                AvailableListValues = new List<KeyValueAssignment>();

                Data = AssignmentItem;
                Option = ValueInfo;

                KeyValue = _Data.Key;

                if (ValueInfo != null)
                {
                    LoadOptionInfoValue();
                }
                else TextboxValue = _Data.Value;

                _DataChanged = false;

                CanChangeAssignmentKey = true;
            }
            catch (Exception Ex)
            {
                throw new Exception("Initialize DataSource Extension-Dialog ViewModel failed!", Ex);
            }
        }
        #endregion

        #region Properties
        public KeyValueAssignment Data
        {
            get { return _Data; }
            set
            {
                _Data = value;
                OnPropertyChanged("Data");
            }
        }

        public OptionInfo Option
        {
            get { return _OptionInfo; }
            set
            {
                _OptionInfo = value;
                OnPropertyChanged("Option");
            }
        }

        public System.Windows.Visibility BooleanValueVisible
        {
            get { return _BooleanValueVisible; }
            set
            {
                _BooleanValueVisible = value;
                OnPropertyChanged("BooleanValueVisible");
            }
        }

        public System.Windows.Visibility ListValueVisible
        {
            get { return _ListValueVisible; }
            set
            {
                _ListValueVisible = value;
                OnPropertyChanged("ListValueVisible");
            }
        }

        public System.Windows.Visibility NumericValueVisible
        {
            get { return _NumericValueVisible; }
            set
            {
                _NumericValueVisible = value;
                OnPropertyChanged("NumericValueVisible");
            }
        }

        public System.Windows.Visibility NumericDoubleValueVisible
        {
            get { return _NumericDoubleValueVisible; }
            set
            {
                _NumericDoubleValueVisible = value;
                OnPropertyChanged("NumericDoubleValueVisible");
            }
        }

        public System.Windows.Visibility TextValueVisible
        {
            get { return _TextValueVisible; }
            set
            {
                _TextValueVisible = value;
                OnPropertyChanged("TextValueVisible");
            }
        }

        public string KeyValue
        {
            get { return _KeyValue; }
            set
            {
                _KeyValue = value;
                OnPropertyChanged("KeyValue");
            }
        }

        public string TextboxValue
        {
            get { return _TextboxValue; }
            set 
            {
                _TextboxValue = value;
                OnPropertyChanged("TextboxValue");
            }
        }

        public bool BoolValue
        {
            get { return _BoolValue; }
            set
            {
                _BoolValue = value;
                OnPropertyChanged("BoolValue");
            }
        }

        public double NumericDoubleValue
        {
            get { return _NumericDoubleValue; }
            set
            {
                _NumericDoubleValue = value;
                OnPropertyChanged("NumericDoubleValue");
            }
        }

        public int NumericValue
        {
            get { return _NumericValue; }
            set
            {
                _NumericValue = value;
                OnPropertyChanged("NumericValue");
            }
        }

        public List<KeyValueAssignment> AvailableListValues
        {
            get { return _AvailableListValues; }
            set
            {
                _AvailableListValues = value;
                OnPropertyChanged("AvailableListValues");
            }
        }

        public string ActiveListValue
        {
            get { return _ActiveListValue; }
            set
            {
                _ActiveListValue = value;
                OnPropertyChanged("ActiveListValue");
            }
        }

        public bool CanChangeAssignmentKey
        {
            get { return _CanChangeAssignmentKey; }
            set
            {
                _CanChangeAssignmentKey = value;
                OnPropertyChanged("CanChangeAssignmentKey");
            }
        }

        #endregion

        #region Helpers
        void Settings_NewEditDataSourceExtensionViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            try
            {
                if (e.PropertyName == "BooleanValueVisible")
                {
                    if (BooleanValueVisible == System.Windows.Visibility.Visible)
                    {
                        ListValueVisible = System.Windows.Visibility.Collapsed;
                        NumericValueVisible = System.Windows.Visibility.Collapsed;
                        NumericDoubleValueVisible = System.Windows.Visibility.Collapsed;
                        TextValueVisible = System.Windows.Visibility.Collapsed;
                    }
                }
                else if (e.PropertyName == "ListValueVisible")
                {
                    if (ListValueVisible == System.Windows.Visibility.Visible)
                    {
                        BooleanValueVisible = System.Windows.Visibility.Collapsed;
                        NumericValueVisible = System.Windows.Visibility.Collapsed;
                        NumericDoubleValueVisible = System.Windows.Visibility.Collapsed;
                        TextValueVisible = System.Windows.Visibility.Collapsed;
                    }
                }
                else if (e.PropertyName == "NumericValueVisible")
                {
                    if (NumericValueVisible == System.Windows.Visibility.Visible)
                    {
                        BooleanValueVisible = System.Windows.Visibility.Collapsed;
                        ListValueVisible = System.Windows.Visibility.Collapsed;
                        NumericDoubleValueVisible = System.Windows.Visibility.Collapsed;
                        TextValueVisible = System.Windows.Visibility.Collapsed;
                    }
                }
                else if (e.PropertyName == "NumericDoubleValueVisible")
                {
                    if (NumericDoubleValueVisible == System.Windows.Visibility.Visible)
                    {
                        BooleanValueVisible = System.Windows.Visibility.Collapsed;
                        NumericValueVisible = System.Windows.Visibility.Collapsed;
                        ListValueVisible = System.Windows.Visibility.Collapsed;
                        TextValueVisible = System.Windows.Visibility.Collapsed;
                    }
                }
                else if (e.PropertyName == "TextValueVisible") 
                {
                    if (TextValueVisible == System.Windows.Visibility.Visible)
                    {
                        BooleanValueVisible = System.Windows.Visibility.Collapsed;
                        NumericValueVisible = System.Windows.Visibility.Collapsed;
                        NumericDoubleValueVisible = System.Windows.Visibility.Collapsed;
                        ListValueVisible = System.Windows.Visibility.Collapsed;
                    }
                }
                else if (e.PropertyName == "ActiveListValue")
                {
                    _DataChanged = true;
                } else if (e.PropertyName == "TextboxValue") {
                    _DataChanged = true;
                }else if (e.PropertyName == "BoolValue")
                {
                    _DataChanged = true;
                } else if (e.PropertyName == "NumericDoubleValue") 
                {
                    _DataChanged = true;
                }
                else if (e.PropertyName == "NumericValue")
                {
                    _DataChanged = true;
                }
            }
            catch (Exception Ex)
            {
                throw new Exception("Error while process event PropertyChanged", Ex);
            }
        }

        void LoadOptionInfoValue()
        {
            try
            {
                AvailableListValues.Clear();

                if (Option.Type == WMSSoft.Lib.Enums.OptionValueType.otBoolean)
                {
                    try
                    {
                        BoolValue = Boolean.Parse(Data.Value);
                    }
                    catch
                    {
                        BoolValue = false;
                    }
                    BooleanValueVisible = System.Windows.Visibility.Visible;
                }
                else if (Option.Type == WMSSoft.Lib.Enums.OptionValueType.otList)
                {
                    foreach (KeyValueAssignment Item in Option.AvailableValues)
                    {
                        AvailableListValues.Add(Item);
                    }
                    if (Data.Value.Length > 0)
                        ActiveListValue = Data.Value;
                    else
                        ActiveListValue = Option.Default;
                    ListValueVisible = System.Windows.Visibility.Visible;
                }
                else if (Option.Type == WMSSoft.Lib.Enums.OptionValueType.otNumeric)
                {
                    int iDefaultValue = 0;
                    if (basic_helpers.isNumeric(Option.Default) == true)
                        iDefaultValue = Convert.ToInt32(Option.Default);

                    if (basic_helpers.isNumeric(Data.Value) == true)
                        NumericValue = Convert.ToInt32(Data.Value);
                    else
                        NumericValue = iDefaultValue;

                }
                else if (Option.Type == WMSSoft.Lib.Enums.OptionValueType.otNumericDouble)
                {
                    double dValue = 0.0;
                    if (basic_helpers.IsDoubleNumeric(Option.Default) == true)
                        dValue = Convert.ToDouble(Option.Default);

                    if (basic_helpers.IsDoubleNumeric(Data.Value) == true)
                        NumericDoubleValue = Convert.ToDouble(Data.Value);
                    else
                        NumericDoubleValue = dValue;
                }
                else if (Option.Type == WMSSoft.Lib.Enums.OptionValueType.otText)
                {
                    TextboxValue = Data.Value;
                    TextValueVisible = System.Windows.Visibility.Visible;
                }
            }
            catch (Exception Ex)
            {
                throw new Exception("Unable to setup Option-Value-Type!", Ex);
            }
        }

        void Settings_NewEditDataSourceExtensionViewModel_ViewModel_CloseClick(object sender, EventArgs e)
        {
            try
            {
                Settings_EditDataSourceExtension parentDlg = null;
                if ((Settings_EditDataSourceExtension)((CDelegatingCommand)Cmd_CloseClick.Command).CommandParameter != null)
                {
                    parentDlg = (Settings_EditDataSourceExtension)((CDelegatingCommand)Cmd_CloseClick.Command).CommandParameter;

                    if (_DataChanged == true)
                    {
                        if (System.Windows.MessageBox.Show(QuestionMessages.QUESTION_CLOSE_DIALOG__LOSE_CHANGES, "Question",
                            System.Windows.MessageBoxButton.YesNo) == System.Windows.MessageBoxResult.No)
                            return;
                    }

                    if (parentDlg != null)
                        parentDlg.Close();
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Error on closing Dialog!", Ex, true, EMessageTypes.Error);
            }
        }

        bool ValidateInputData()
        {
            if ((Option == null && KeyValue.Length > 0 &&  TextboxValue.Length > 0) | 
                (Option != null && ((Option.Type == WMSSoft.Lib.Enums.OptionValueType.otBoolean) | 
                (Option.Type == WMSSoft.Lib.Enums.OptionValueType.otText && TextboxValue.Length >= Option.MinLengthValue && TextboxValue.Length <= Option.MaxLengthValue) |
                (Option.Type == WMSSoft.Lib.Enums.OptionValueType.otNumeric && NumericValue >= Option.MinNumValue && NumericValue <= Option.MaxNumValue) |
                (Option.Type == WMSSoft.Lib.Enums.OptionValueType.otNumericDouble && NumericDoubleValue >= Option.MinNumDblValue && NumericDoubleValue <= Option.MaxNumDblValue) |
                (Option.Type == WMSSoft.Lib.Enums.OptionValueType.otList && ActiveListValue.Length > 0)
                ))) 
            {
                return true;
            } else return false;
        }

        void Settings_NewEditDataSourceExtensionViewModel_ViewModel_OkClick(object sender, EventArgs e)
        {
            try
            {
                Settings_EditDataSourceExtension parentDlg = null;
                if ((Settings_EditDataSourceExtension)((CDelegatingCommand)Cmd_OkClick.Command).CommandParameter != null)
                {
                    parentDlg = (Settings_EditDataSourceExtension)((CDelegatingCommand)Cmd_OkClick.Command).CommandParameter;

                    if (ValidateInputData() == true)
                    {                        
                        Data.Key = _KeyValue;
                        if (Option == null) Data.Value = TextboxValue;
                        if (Option != null && Option.Type == WMSSoft.Lib.Enums.OptionValueType.otBoolean) Data.Value = Convert.ToString(BoolValue);
                        if (Option != null && Option.Type == WMSSoft.Lib.Enums.OptionValueType.otList) Data.Value = ActiveListValue;
                        if (Option != null && Option.Type == WMSSoft.Lib.Enums.OptionValueType.otNumeric) Data.Value = Convert.ToString(NumericValue);
                        if (Option != null && Option.Type == WMSSoft.Lib.Enums.OptionValueType.otNumericDouble) Data.Value = Convert.ToString(NumericDoubleValue);
                        if (Option != null && Option.Type == WMSSoft.Lib.Enums.OptionValueType.otText) Data.Value = TextboxValue;

                        if (parentDlg != null)
                        {
                            parentDlg.DialogResult = true;

                            parentDlg.Close();
                        }
                    }
                    else
                    {
                        System.Windows.MessageBox.Show(LanguageStrings.GetText(InfoMessages.INFO_MODULES_DATASOURCE_ASSIGNMENT_INPUT_INVALID, WMSSoft.Lib.Enums.EStringTypes.stMessage), 
                                                       LanguageStrings.GetText("Info"), System.Windows.MessageBoxButton.OK);
                    }
                }                
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Error on accept Changes!", Ex, true, EMessageTypes.Error);
            }
        }
        #endregion
    }
}

